// pages/plan/plan.js
Page({
  data: {
    newPlanText: '',
    selectedCategory: 'health',
    categories: [
      { id: 'health', name: '健康' },
      { id: 'study', name: '学习' },
      { id: 'work', name: '工作' },
      { id: 'life', name: '生活' }
    ],
    activePlans: [
      {
        id: 1,
        text: '每周健身3次',
        category: 'health',
        categoryName: '健康',
        startDate: '2023-11-01',
        progress: 65
      },
      {
        id: 2,
        text: '每天阅读30分钟',
        category: 'study',
        categoryName: '学习',
        startDate: '2023-11-10',
        progress: 40
      }
    ]
  },

  // 更新计划文本
  updatePlanText(e) {
    this.setData({
      newPlanText: e.detail.value
    });
  },

  // 选择分类
  selectCategory(e) {
    this.setData({
      selectedCategory: e.currentTarget.dataset.id
    });
  },

  // 创建新计划
  createPlan() {
    if (!this.data.newPlanText.trim()) {
      wx.showToast({
        title: '请输入计划内容',
        icon: 'none'
      });
      return;
    }

    const newPlan = {
      id: Date.now(),
      text: this.data.newPlanText,
      category: this.data.selectedCategory,
      categoryName: this.data.categories.find(item => item.id === this.data.selectedCategory).name,
      startDate: new Date().toISOString().split('T')[0],
      progress: 0
    };

    this.setData({
      activePlans: [newPlan, ...this.data.activePlans],
      newPlanText: ''
    });

    wx.showToast({
      title: '计划已添加',
      icon: 'success'
    });
  },

  // 进度更新（示例方法）
  updateProgress(e) {
    const { id } = e.currentTarget.dataset;
    // 实际开发中这里应该有更复杂的逻辑
    const updatedPlans = this.data.activePlans.map(item => {
      if (item.id === id) {
        return { ...item, progress: Math.min(item.progress + 10, 100) };
      }
      return item;
    });
    this.setData({ activePlans: updatedPlans });
  }
});